<?php

namespace app\agent\controller;

use app\common\controller\Agent;
use app\common\model\Images;
use app\common\model\OrderAftersalesRecord;
use think\Db;
use think\Exception;

/**
 * 订单佣金
 *
 * @icon fa fa-user
 */
class OrderCommission extends Agent
{

    protected $relationSearch = true;
    protected $multiFields = 'status';

    protected $model = null;
    protected $agent_id = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('OrderCommission');
        $agent_info = session('agent');
        $this->agent_id = $agent_info['id'];
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $list = $this->model->with(['member','car','shop','order','orderGoods'])->where($where)->where(['order_commission.agent_id'=>$this->agent_id])->order($sort, $order)->paginate($limit)->each(function ($item){
                $item['platform_income'] = 0.000;
                if ($item['status'] != 2){
                    $item['platform_income'] = sprintf("%.3f",($item['pay_amount'] - $item['cost'] - $item['money']));
                }else{
                    $item['money'] = 0;
                }
                return $item;
            });
            if (!isset($filter['status'])) { //全统计
                $pay_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0,1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('pay_amount');
                $cost_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0,1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('cost');
                $money_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0,1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('order_commission.money');
            }elseif ($filter['status'] == 0){
                $pay_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0])->where(['order_commission.agent_id'=>$this->agent_id])->sum('pay_amount');
                $cost_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0])->where(['order_commission.agent_id'=>$this->agent_id])->sum('cost');
                $money_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[0])->where(['order_commission.agent_id'=>$this->agent_id])->sum('order_commission.money');
            }elseif ($filter['status'] == 1){
                $pay_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('pay_amount');
                $cost_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('cost');
                $money_amount = $this->model->with(['member','car','shop','orderGoods'])->where($where)->where('fa_order_commission.status','in',[1])->where(['order_commission.agent_id'=>$this->agent_id])->sum('order_commission.money');
            }elseif ($filter['status'] == 2){
                $pay_amount = 0;
                $cost_amount = 0;
                $money_amount = 0;
            }
            $count = [['name'=>'实付总金额','value'=>sprintf("%.3f",$pay_amount)],['name'=>'累计总成本','value'=>sprintf("%.3f",$cost_amount)],['name'=>'代理总佣金','value'=>sprintf("%.3f",$money_amount)]];
            $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>['count'=>$count]);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function record($ids=null)
    {
        $list = OrderAftersalesRecord::where('order_aftersales_id',$ids)->order('createtime desc')->select()->toArray();
        $this->view->assign("list", $list);
        return $this->view->fetch();
    }


}
